package com.face.dao;

import com.face.common.Page;
import com.face.entity.Role;
import com.face.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDao {

    @Autowired
    private RoleMapper roleMapper;

    public Page<Role> findPage(String shopId, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Role> list = roleMapper.findPage(shopId);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }

    public Page<Role> findPage(String shopId, Integer status, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Role> list = roleMapper.findPageByStatus(shopId, status);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }
    public Page<Role> findPage(String shopId, String name, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Role> list = roleMapper.findPageByName(shopId, name);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }
    public Page<Role> findPage(String shopId, String name, Integer status, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Role> list = roleMapper.findPageByNameAndStatus(shopId, name, status);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }

    public Integer save(Role role) {
        return roleMapper.save(role);
    }

    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    public void update(Role role) {
        roleMapper.update(role);
    }

    public void updateStatus(Integer id, Integer status) {
        roleMapper.updateStatus(id,status);
    }

    public Role findByName(String name, String shopId) {
        return roleMapper.findByName(name, shopId);
    }

    public Role findByOpenid(String username){
        return roleMapper.findByOpenid(username);
    }
}
